﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Data;
using System.Data.Common;
using System.IO;

namespace FmcWebsite.Core
{
    public class Author
    {
        private int id = -1;
        private string name = "";
        private string description = "";
        private Image image = null;

        public Author()
        {
        }

        public Author(DataRow aut_row)
        {
            id = aut_row.Field<int>("author_id");
            name = aut_row.Field<string>("name");
            description = aut_row.Field<string>("description");
            if (aut_row["picture"] != null)
            {
                MemoryStream mem = new MemoryStream((byte[])aut_row["picture"]);
                image = Image.FromStream(mem);
            }
        }

        public int Id
        {
            get { return id; }
        }

        public Image Picture
        {
            get { return image; }
            set { image = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public bool IsNull()
        {
            return (id == -1);
        }

        public override string ToString()
        {
            return Multilang.Extract(name, "EN");
        }

        public override bool Equals(object obj)
        {
            return (obj is Author) && (((Author)obj).id == this.id);
        }

        public override int GetHashCode()
        {
            return id;
        }

        private IEnumerable<DbParameter> GetParams()
        {
            MemoryStream mem = new MemoryStream();
            image.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg);
            mem.Position = 0;

            return new DbParameter[]
            {
                DBConnect.Database.CreateParameter("@pic", mem.ToArray()),
                DBConnect.Database.CreateParameter("@name", name),
                DBConnect.Database.CreateParameter("@desc", description)
            };
        }

        public void Update()
        {
            if (id != -1)
            {
                DBConnect.Database.ExecDDL(
                    "UPDATE author SET picture=@pic, name=@name, description=@desc WHERE author_id=" + id.ToString(),
                    GetParams());
            }
        }

        public void Insert()
        {
            if (id == -1)
            {
                DBConnect.Database.ExecDDL(
                    "INSERT INTO author SET (name,description,picture) VALUES (@name,@desc,@pic)",
                    GetParams());

                var tbl = DBConnect.Database.Select("SELECT max(author_id) FROM author");
                id = tbl.Rows[0].Field<int>(0);
            }
        }

        static public Author ById(int id)
        {
            var tbl = DBConnect.Database.Select("SELECT * FROM author WHERE author_id = " + id.ToString());
            return new Author(tbl.Rows[0]);
        }

        static public IEnumerable<Author> GetAll()
        {
            var tbl = DBConnect.Database.Select("SELECT * FROM author");
            foreach (DataRow r in tbl.Rows)
                yield return new Author(r);
        }
    }
    
}
